package com.dart.sys.web.controller;

import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.List;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.i18n.CookieLocaleResolver;

import com.alibaba.fastjson.JSON;
import com.dart.commons.base.web.BaseController;
import com.dart.commons.i18n.Resources;
import com.dart.framework.common.kaptcha.impl.DefaultKaptcha;
import com.dart.sys.entity.SysOperateLog;
import com.dart.sys.entity.User;
import com.dart.sys.mq.produer.queue.TestQueueSender;
import com.dart.sys.mq.produer.topic.TestTopicSender;

@Controller
@RequestMapping("/test")
public class TestController extends BaseController {
	
	@Autowired
	private TestQueueSender testQueueSender;
	
	@Autowired
	private TestTopicSender testTopicSender;
	
	@Autowired 
	private CookieLocaleResolver resolver;
	
	@Autowired
	private DefaultKaptcha kaptcha;
	
	@RequestMapping(value = "/kaptcha")
	public void kaptcha() {
		ServletOutputStream out = null ;
		try {
			response.setDateHeader("Expires", 0);
			response.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");
			response.addHeader("Cache-Control", "post-check=0, pre-check=0");
			response.setHeader("Pragma", "no-cache");
			response.setContentType("image/jpeg");
			String capText = kaptcha.createText();
			request.getSession().setAttribute(com.dart.framework.common.kaptcha.Constants.KAPTCHA_SESSION_KEY, capText);
			BufferedImage bi = kaptcha.createImage(capText);
			out = response.getOutputStream();
			ImageIO.write(bi, "jpg", out);
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if(null != out) {
					out.flush();
					out.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
	@RequestMapping("testQueueSender")
	public void testQueueSender() {
		this.testQueueSender.sendText("测试Queue消息...");
	}
	
	@RequestMapping("testQueueSenderObject")
	public void testQueueSenderObject() {
		SysOperateLog entity = new SysOperateLog() ;
		entity.setOperate("你好");
		
		this.testQueueSender.sendObject(entity);
	}
	
	@RequestMapping("testTopicSender")
	public void testTopicSender() {
		this.testTopicSender.send("测试Topic消息...");
	}

	/**
	 * 接收集合参数
	 * 参数内容：
	 * [{
	 *     "account": "ac1"
	 * },
	 * {
	 *     "account": "ac2"
	 * }]
	 * @param users
	 */
	@RequestMapping("listParams")
	public void listParams(@RequestBody List<User> users) {
		for (User user : users) {
			System.out.println(JSON.toJSON(user));
		}
	}
	
	@RequestMapping("i18n")
	public void i18n(HttpServletRequest request, HttpServletResponse response) {
		
		System.out.println(viewPrefix);
		
		System.out.println("----" + request);
		System.out.println("----" + response);
		System.out.println("----" + session);
		System.out.println("----" + application);
		
		String message = Resources.getMessage("EMAIL.SET_COPYTO", "123");
		try {
			response.setContentType("text/html;charset=utf-8");
			response.getWriter().print(message);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	@RequestMapping("asert")
	public void asert() {
		Assert.notNull("Serializer must not be null!");
	}
	
	@RequestMapping("i18n_tag")
	public String i18n_tag() {
		return "i18n";
	}
	
}
